<?php 
/*后台登录*/
namespace app\admin\controller;

use think\Controller;

class Hslogin extends Controller{
    protected function _initialize()
    {
        parent::_initialize();
        //站点信息
        $web = db('system')->where(['name'=>'site_config'])->value('value');
        $this->assign('web',unserialize($web));
    }
    public function index(){
        if(session('?admin_id')) $this->redirect('index/index');
       	return view('index');
    }
    public function login(){
        if (request()->isAjax()) {
        	$username = input('username');	$password = input('password');	$captcha = input('captcha');
        	if(!captcha_check($captcha)) $this->error('验证码错误或过期');

        	$admin_user = db('admin_user')->where(['username'=>$username])->find();
        	if($admin_user){
                if (!password_verify($password, $admin_user['password'])) $this->error('密码错误');
        		if($admin_user['status'] != 1) $this->error('当前用户已禁用');
                //value(),仅返回当前字段值
                if(db('auth_group')->where(['id'=>$admin_user['group_id']])->value('status') == 0) $this->error('当前用户组已禁用');

        		session('admin_id', $admin_user['id']);// 赋值（当前作用域）
        		session('admin_name', $admin_user['username']);
                session('group_id', $admin_user['group_id']);
                session('admin_img',$admin_user['img']);//头像
                db('admin_user')->where('id',$admin_user['id'])->update(
                	[
                		'last_login_time' => date('Y-m-d H:i:s'),
                		'last_login_ip'   => request()->ip(),
                	]
                );
                $this->success('登录成功', 'index/index');
        	}else{
        		$this->error('用户名错误');
        	}
        }
    }
    /**
     * 退出登录
     */
    public function logout()
    {
        session('admin_id',null);
        session('group_id',null);
        session('admin_name',null);
        session('admin_img',null);
        $this->success('退出成功', 'Hslogin/index');
    }
}